<%-- 
    Document   : ProductEdit
    Created on : Mar 31, 2014, 11:23:31 AM
    Author     : asmaa
--%>

<!DOCTYPE html>
        <%@include file="headerAdmin.jsp" %>  

        <sql:query dataSource="${conn}" var="result">
            SELECT Product.*,Category.catName from Product,Category where Product.id=? and Category.id=Product.categoryId
            <sql:param value="${param.id}" />
        </sql:query>
        <script>
            function gobackProd(id)
            {
                location.href = "productlistadminview.jsp?id=" + id;
            }
            function validateProductEdit()
            {
                var name = document.forms["add"]["name"].value;
                var regexName = /^[a-zA-Z0-9\s]+$/;
                if (!regexName.test(name))
                {
                    alert("Name should not contain special character");
                    document.add.name.focus();
                    return false;
                }
                var description = document.forms["add"]["description"].value;
                var regexName = /^[a-zA-Z0-9\s\,\\]+$/;
                if (!regexName.test(name))
                {
                    alert("Name should not contain special character");
                    document.add.description.focus();
                    return false;
                }
                
                var quantity = document.forms["add"]["quantity"].value;
                var regexQuantity = /^\d{1,6}$/;
                if (!regexQuantity.test(quantity))
                {
                    alert("Quantity should be decimal number");
                    document.add.quantity.focus();
                    return false;
                }
                var price = document.forms["add"]["price"].value;
                var regexPrice = /^\d*\.?\d*$/;
                if (!regexPrice.test(price))
                {
                    alert("Price can be float or integer number ");
                    document.add.price.focus();
                    return false;
                }
            }
        </script>
        <div class="top_bg">
            <div class="wrap">
                <div class="main_top">
                    <h2 class="style">Edit Product</h2>
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-6" style="margin-left: 60px;margin-top: 20px">
                    <c:forEach var="row" items="${result.rows}">
                        <form role="form" action="EditProduct" name="add" method="POST" onsubmit="return validateProductEdit()">

                            <div class="form-group">
                                <img src="DisplayImage?id=${row.id}" name="image" style="height:250px;weidth:300px;" alt="Responsive image"><br>
                                <input type="hidden" value="${row.id}" name="productId">
                                <input type="hidden" value="${row.categoryId}" name="catId">
                            </div>
                            <div class="form-group">
                                <label for="name">Name</label><br>
                                <input type="text" name="name" required value="${row.name}">
                            </div>
                            <div class="form-group">
                                <label for="description">Description</label><br>
                                <textarea name="description" required >${row.description}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="quantity">Quantity</label><br>
                                <input type="text" required name="quantity" value="${row.quantity}">
                            </div>
                            <div class="form-group">
                                <label for="price">Price</label><br>
                                <input type="text" required name="price" value="${row.price}">
                            </div>
                            <sql:query dataSource="${conn}" var="cat">
                                SELECT Category.* FROM Category
                                WHERE id NOT
                                IN (

                                SELECT categoryId
                                FROM Product
                                WHERE categoryId =${row.categoryId});
                            </sql:query>
                            <div class="form-group">
                                <label for="category">Categories:</label><br>
                                <select name="categoryID" >

                                    <option name="categoryId" value="${row.catrgoryId}">
                                        <c:out value="${row.catName}"/>
                                    </option>
                                    <c:forEach var="category" items="${cat.rows}">
                                        <option name="categoryId" value="${category.id}">
                                            <c:out value="${category.catName}"/>
                                        </option>
                                    </c:forEach>
                                </select> <br>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-success" />Submit</button> 
                                <button type="button" onclick="gobackProd(${row.categoryId})" class="btn btn-sm btn-success">Back</button>
                            </div>
                        </c:forEach>   
                    </form>
                </div>
            </div>
        </div>
        <%@include file="footer.jsp" %>
